#!/bin/sh
# wine-select.sh - Selects the default wine version.

# Copyright 2019 orbea
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

IFS=' 	
'

set -eu

die () {
  ret="$1"; shift
  case "$ret" in
    : ) printf %s\\n "$@" >&2; return 0 ;;
    0 ) printf %s\\n "$@" ;;
    * ) printf %s\\n "$@" >&2 ;;
  esac
  exit "$ret"
}

doinst () {
  if [ -x "$udd" ] && [ -f "$udd" ]; then
    "$udd" -q /usr/share/applications
  fi
}

default='/var/lib/wine/default'
udd='/usr/bin/update-desktop-database'

usage="wine-select.sh - Selects the default wine version.

  Usage: wine-select.sh [OPTIONS]
    -h, --help,        Show this help message.
    -u, --uninstall,   Remove existing symlinks.

  Files:
    $default : List of symlinks for the default wine version."

list=
files=
choice=
uninstall=

while [ $# -gt 0 ]; do
  option="$1"
  shift
  case "$option" in
    -- ) break ;;
    -u|--uninstall ) uninstall=1 ;;
    -h|--help ) die 0 "$usage" ;;
    * ) die 1 "Unrecognized option '$option', use -h for help." ;;
  esac
done

[ "$(id -u)" != 0 ] && die 1 'ERROR: User is not root.'

cd /opt

if [ -z "${uninstall}" ]; then
  printf %s\\n '' 'Select the default wine install:' ''

  n=0

  for i in wine*/*; do
    [ -d "$i" ] || continue
    printf %s\\n "  $n ) $i" | tr '/' '-'
    eval "wine$n=\"$i\""
    list="${list} $n"
    n=$((n+1))
  done

  printf %s\\n ''

  read -r selection

  eval "set -- $list"
  for i do
    case "$selection" in
      "$i" ) eval "choice=\"\${wine$selection}\""; break ;;
      * ) continue ;;
    esac
  done

  if [ -z "${choice}" ]; then
    die 1 'ERROR: Invalid wine version chosen.'
  fi
fi

mkdir -p /var/lib/wine

if [ -f "$default" ] && [ -r "$default" ]; then
  while read -r line; do
    if [ -h "$line" ]; then
      rm -f -- "$line"
    fi
  done < "$default"
fi

if [ "${uninstall}" ]; then
  doinst
  rm -f "$default"
  exit 0
fi

ln='/usr/bin/ln'
winedir="/opt/$choice"

for bin in "$winedir"/bin/*; do
  if [ ! -e "/usr${bin#$winedir}" ]; then
    "$ln" -sf "$bin" /usr/bin
    files="${files} /usr${bin#$winedir}"
  else
    die : "WARNING: File '$bin' already exists."
  fi
done

for libpath in lib lib64; do
  [ -d "$winedir/$libpath" ] || continue
  mkdir -p "/usr/$libpath/wine/fakedlls"
  for lib in "$winedir/$libpath"/* "$winedir/$libpath"/wine/* \
        "$winedir/$libpath"/wine/fakedlls/*; do
    if [ -f "$lib" ]; then
      if [ ! -e "/usr/${lib#$winedir}/wine" ]; then
        libdir="${lib%/*}"
        "$ln" -sf "$lib" "/usr${libdir#$winedir}"
        files="${files} /usr${lib#$winedir}"
      else
        die : "WARNING: File '$lib' already exists."
      fi
    fi
  done
done

for man in "$winedir"/man/*/*.gz "$winedir"/man/*/*/*.gz; do
  if [ ! -e "/usr/${man#$winedir}" ]; then
    mandir="${man%/*}"
    mkdir -p "/usr/${mandir#$winedir}"
    "$ln" -sf "$man" "/usr${mandir#$winedir}"
    files="${files} /usr${man#$winedir}"
  else
    die : "WARNING: File '$man' already exists."
  fi
done

for misc in include/wine share/wine share/applications/wine.desktop; do
  if [ ! -e "/usr/$misc" ]; then
    "$ln" -sf "$winedir/$misc" "/usr/$misc"
    files="${files} /usr/$misc"
  else
    die : "WARNING: File '$winedir/$misc' already exists."
  fi
done

if [ "${files}" ]; then
  doinst
  printf %s\\n "${files# }" | tr ' ' '\n' | sort > "$default"
  printf %s\\n '' "Wine version '$choice' selected." '' | tr '/' '-'
fi

exit 0
